<?php
declare(strict_types=1);

namespace hangpu8\hpcms\controller;

use hangpu8\hpcms\Utils\Json;
use think\exception\ValidateException;

class AuthRule extends Base
{
    public function index()
    {
        $AuthRuleService = new \hangpu8\hpcms\service\AuthRule;
        $list = $AuthRuleService->getlist(['pid'=> '']);
        $builder = new \hangpu8\hpcms\Utils\Builder\ListBuilder;
        $data = $builder
            ->addColumn('rightButtonList', '操作', [
                'type' => 'template',
                // 'width' => '200',
                'template' => 'rightButtonList'
            ])
            ->addTopButton('add', '添加', [
                'title'             => '添加数据',
                'api'               => 'hangpu8/AuthRule/add',
            ])
            ->addRightButton('edit', '修改',[
                'api' => 'hangpu8/AuthRule/edit',
                'title' => '修改',
                'apiPrefix' => ['path'],
            ])
            ->addRightButton('delete', '删除', [
                'api' => 'hangpu8/AuthRule/del',
                'method' => 'delete',
                'title' => '确认要删除该数据吗？',
                'modalType' => 'confirm',
                'width' => '600',
                'okText' => '确认删除',
                'cancelText' => '取消操作',
                'content' => '<p>删除该数据不可恢复</p>',
                'apiPrefix' => ['path'],
            ])
            ->addColumn('path', '路由')
            ->addColumn('version', '版本',[
                'width'     => '100',
            ])
            ->addColumn('title', '名称')
            ->addColumn('show_text', '状态',[
                'width'     => '80',
            ])
            ->addColumn('type_text', '菜单类型')
            ->addColumn('method', '请求类型')
            ->setData($list['data'])
            ->create();
            $data['children']       = [
                'api'               => '/hangpu8/AuthRule/getChildren',
                'method'            => 'get',
            ];
        return Json::successful($data);
    }

    /**
     * 获取子节点数据
     *
     * @param [type] $path
     * @return void
     */
    public function getChildren($path)
    {
        $authRuleService = new \hangpu8\hpcms\service\AuthRule;
        $list = $authRuleService->getlist(['pid'=> $path]);
        return Json::successful($list['data']);
    }

    /**
     * 添加数据
     *
     * @return void
     */
    public function add()
    {
        $authRuleService = new \hangpu8\hpcms\service\AuthRule;
        if (request()->isPost())
        {
            $post = request()->post();
            try {
                validate(\hangpu8\hpcms\validate\AuthRule::class)
                    ->check($post);
            } catch (ValidateException $e) {
                list($code, $msg) = explode("\n", $e->getMessage());
                return Json::fail($msg,(int)$code);
            }
            try {
                $authRuleService->create($post);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $getFormData = $authRuleService->getFormData();
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('post')
            ->addRow('pid','cascader','父级菜单',[],[
                'props'             => [
                    'props'         => [
                        'checkStrictly' => true,
                    ],
                ],
                'options'           => $getFormData['parent'],
                'placeholder'       => '如不选择则是顶级菜单',
            ])
            ->addRow('title','input','菜单名称')
            ->addRow('path','input','菜单地址','',[
                'placeholder'   => '应用/控制器/方法',
            ])
            ->addRow('desc','input','菜单描述','')
            ->addRow('method','checkbox','请求类型',['GET'],[
                'options'   => $getFormData['methods'],
            ])
            ->addRow('type','select','菜单类型','',[
                'options'   => $getFormData['type'],
                'control'   => [
                    [
                        'value' => 'component',
                        'rule'  => [
                            [
                                'type'  => 'input',
                                'title' => '远程组件',
                                'field' => 'remote',
                                'value' => '',
                                'props' => [
                                    'type'  => 'text',
                                    'placeholder'   => '请输入远程组件',
                                ],
                            ],
                        ],
                    ],
                ],
            ])
            ->addRow('version','input','菜单版本','v1')
            ->addRow('icon','input','菜单图标','')
            ->addRow('show','radio','显示隐藏',1,[
                'options'   => $getFormData['show'],
            ])
            ->addRow('sort','input','菜单排序',100)
            ->create();
            return Json::successful($data);
        }
    }

    /**
     * 修改数据
     *
     * @param [type] $path
     * @return void
     */
    public function edit($path)
    {
        $authRuleService = new \hangpu8\hpcms\service\AuthRule;
        if (request()->isPost())
        {
            $post = request()->post();
            try {
                validate(\hangpu8\hpcms\validate\AuthRule::class)
                    ->check($post);
            } catch (ValidateException $e) {
                list($code, $msg) = explode("\n", $e->getMessage());
                return Json::fail($msg,(int)$code);
            }
            try {
                $authRuleService->saveUpdate($post,['path'=> $path]);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $form = $authRuleService->getForm(['path'=> $path]);
            $getFormData = $authRuleService->getFormData();
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('post')
            ->addRow('pid','cascader','父级菜单',[],[
                'props'             => [
                    'props'         => [
                        'checkStrictly' => true,
                    ],
                ],
                'options'           => $getFormData['parent'],
                'placeholder'       => '如不选择则是顶级菜单',
            ])
            ->addRow('title','input','菜单名称')
            ->addRow('path','input','菜单地址','',[
                'placeholder'   => '应用/控制器/方法',
            ])
            ->addRow('desc','input','菜单描述','')
            ->addRow('method','checkbox','请求类型',['GET'],[
                'options'   => $getFormData['methods'],
            ])
            ->addRow('type','select','菜单类型','',[
                'options'   => $getFormData['type'],
                'control'   => [
                    [
                        'value' => 'component',
                        'rule'  => [
                            [
                                'type'  => 'input',
                                'title' => '远程组件',
                                'field' => 'remote',
                                'value' => '',
                                'props' => [
                                    'type'  => 'text',
                                    'placeholder'   => '请输入远程组件',
                                ],
                            ],
                        ],
                    ],
                ],
            ])
            ->addRow('version','input','菜单版本','v1')
            ->addRow('icon','input','菜单图标','')
            ->addRow('show','radio','显示隐藏',1,[
                'options'   => $getFormData['show'],
            ])
            ->addRow('sort','input','菜单排序',100)
            ->setFormData($form)
            ->create();
            return Json::successful($data);
        }
    }

    /**
     * 删除数据
     *
     * @param [type] $path
     * @return void
     */
    public function del($path)
    {
        $authRuleService = new \hangpu8\hpcms\service\AuthRule;
        try {
            $authRuleService->del($path);
            return Json::successful([],'删除成功');
        } catch (\Throwable $e) {
            return Json::fail($e->getMessage(),$e->getCode());
        }
    }
}
